package DBUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtil {
    //设置数据库参数
    private static final  String driver = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/information?useSSL=true&characterEncoding=utf-8&serverTimezone=GMT";
    private static final String USER = "information";
    private static final String PASSWORD = "123456";
    private static Connection conn = null;

    //connetcDB方法：连接数据库
    public static void connetcDB() {
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(URL,USER,PASSWORD);
        }
            catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }

    }

    //getConnection方法：返回所建立的数据库连接，即：Connection对象
    public static Connection getConnection() {
        return conn;
    }
    public static void closeConnection(){
        try {
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    DBUtil(){
    }
}